package com.controller;

import com.constant.Constant;
import com.entity.Comment;
import com.entity.User;
import com.service.CommentService;
import com.service.PraiseService;
import com.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.vo.CommentVo;

import java.util.List;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/comment")

public class CommentCtl {
    @Autowired
    CommentService commentService;
    @Autowired
    PraiseService praiseService;

    //@李奇洲dd
    //发表评论
    @RequestMapping("/sendComs.do")
    @ResponseBody
    public AjaxResult sendComs(HttpSession session, @RequestParam("resource_id") Integer resourceId, String context) {
        return commentService.insertComment(session, resourceId, context);
    }

    //李奇洲 查询点赞记录
    @ResponseBody
    @RequestMapping("/getPraCount.do")
    public AjaxResult getPraCount(@RequestParam("comment_id") Integer commentID) {
        CommentVo vo = praiseService.findPrasieCountByUidAndCommentID(commentID);
        System.out.println("当前赞数量=" + vo.getPraise_count());
        AjaxResult ajaxResult = new AjaxResult(true, null, vo.getPraise_count());
        return ajaxResult;
    }

    //吴彬彬     根据条件分页查询待审核评论
    @RequestMapping("/findByChapterId.do")
    @ResponseBody
    public AjaxResult findByChapterId(CommentVo commentVo) {
        AjaxResult result = null;
        System.out.println("PageNo====" + commentVo.getPageNo());
        try {
            PageInfo<List> info = commentService.findByChapterId(commentVo);
            result = new AjaxResult(true, "上传成功", info);

        } catch (Exception e) {
            result.setSuccess(false);
            e.printStackTrace();
            e.getMessage();
        }

        return result;
    }

    //吴彬彬  通过审核、禁用评论
    @RequestMapping("/toggle.do")
    public void updateStatus(Integer id, Integer status) {
        int i = 0;
        try {
            i = commentService.updateStatus(id, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (i > 0) {
            System.out.println("33333");
        } else {
            System.out.println("44444");
        }


    }

    //吴彬彬   根据条件分页查询已审核评论(启用、禁用)
    @RequestMapping("/findComs.do")
    @ResponseBody
    public AjaxResult findComs(CommentVo commentVo) {

        System.out.println(commentVo.toString());
        AjaxResult result = null;
        try {
            PageInfo<List> coms = commentService.findComs(commentVo);
            result = new AjaxResult(true, "上传成功", coms);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg(e.getMessage());
        }
        System.out.println(result.getObj().toString());
        return result;
    }

    @RequestMapping("/loadResComs.do")
    @ResponseBody
    //分页查询所有评论 李奇洲
    public AjaxResult loadResComs(@RequestParam("resource_id") Integer resourceId, Integer pageNo) {
        System.out.println(resourceId);
        return commentService.findAllComment(resourceId, pageNo);
    }
}

